package com.hjf.booking.security.config;

import com.hjf.booking.security.filter.MyUsernamePasswordAuthenticationFilter;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.logout.LogoutFilter;

/**
 * @author hjf
 * @date 2021-03-17 9:47
 */
public class LoginConfigure<T extends LoginConfigure<T, B>, B extends HttpSecurityBuilder<B>> extends AbstractHttpConfigurer<T, B> {

    private MyUsernamePasswordAuthenticationFilter authenticationFilter;

    //引入我们自定义的过滤器
    public LoginConfigure() {
        this.authenticationFilter = new MyUsernamePasswordAuthenticationFilter();
    }

    //配置过滤器相关配置
    @Override
    public void configure(B http) throws Exception {
        authenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        authenticationFilter.setAuthenticationFailureHandler(new LoginFailureHandler());
        MyUsernamePasswordAuthenticationFilter filter = postProcess(authenticationFilter);
        http.addFilterAfter(filter,LogoutFilter.class);
    }

    public LoginConfigure<T,B> loginSuccessHandler(AuthenticationSuccessHandler authSuccessHandler){
        authenticationFilter.setAuthenticationSuccessHandler(authSuccessHandler);
        return this;
    }
}
